package com.immunology.servlets;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.model.MappingPages;
import com.immunology.service.SurveyAnswerService;
import com.immunology.service.SurveyService;

public class SelectSurveyOperationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SelectSurveyOperationServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");

		Enumeration<?> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String parameter = (String) enumeration.nextElement();
			if (parameter.equals("addSurvey")) {
				request.getRequestDispatcher(MappingPages.ADD_SURVEY_JSP)
						.forward(request, response);
			} else {
				int surveyId = Integer.valueOf(parameter);

				SurveyService surveyService = SurveyService.getInstance();
				Map<String, List<Integer>> surveyResultsMap = surveyService
						.getSurveyResultMap(surveyId);

				// SyndromeService syndromeService =
				// SyndromeService.getInstance();
				// int syndromeId = (Integer) getServletContext().getAttribute(
				// "syndromeId");
				// Syndrome syndrome =
				// syndromeService.getSyndromeById(syndromeId);
				// String syndromeName = syndrome.getName();

				SurveyAnswerService surveyAnswerService = SurveyAnswerService
						.getInstance();
				List<String> resultList = surveyAnswerService
						.getSumForComplaintTypeValue(surveyResultsMap);

				int difficultyDegree = Integer.valueOf(resultList.get(2));
				surveyService.setDifficultyDegreeBySurveyId(surveyId,
						difficultyDegree);

				// request.setAttribute("syndromeName", syndromeName);
				request.setAttribute("surveyResultsMap", surveyResultsMap);
				request.setAttribute("resultList", resultList);

				request.getRequestDispatcher(
						MappingPages.SHOW_SURVEY_JSP).forward(request,
						response);
			}
		}

	}

}
